﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WindowsFormsUseCaseEngine.cs" company="">
//   
// </copyright>
// <summary>
//   The windows forms use case engine.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
    using System;
    using System.Windows.Forms;

    using Libium.Adapters;
    using Libium.Bindings;
    using Libium.Controls;

    /// <summary>
    /// The windows forms use case engine.
    /// </summary>
    public class WindowsFormsUseCaseEngine : BaseUseCaseEngine
    {
        #region Public Methods and Operators

        /// <summary>
        /// The execute modal use case.
        /// </summary>
        /// <param name="useCase">
        /// The use case.
        /// </param>
        /// <returns>
        /// The <see cref="CommandDefinition"/>.
        /// </returns>
        public override CommandDefinition ExecuteModalUseCase(IUseCase useCase)
        {
            var messageUseCase = useCase as IMessageUseCase;
            if (messageUseCase != null)
            {
                // TODO Develop message form
                MessageBox.Show(messageUseCase.Text, "Error", MessageBoxButtons.OK);
                return StandardCommandDefinitions.OkCommand;
            }

            return base.ExecuteModalUseCase(useCase);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The create view for use case.
        /// </summary>
        /// <param name="useCase">
        /// The use case.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// </exception>
        protected override object CreateViewForUseCase(IUseCase useCase)
        {
            if (useCase is IOpenFileUseCase)
            {
                return new OpenFileDialog();
            }

            if (useCase is ISaveFileUseCase)
            {
                return new SaveFileDialog();
            }

            if (useCase is IMessageUseCase)
            {
                return new MessageForm();
            }

            throw new NotSupportedException("Can't create view for use case <" + useCase + ">");
        }

        /// <summary>
        /// The initialize bind service.
        /// </summary>
        /// <param name="bindService">
        /// The bind service.
        /// </param>
        protected override void InitializeBindService(CompoundBindService bindService)
        {
            bindService.AdaptService = new WindowsFormsAdaptService();
            bindService.AddBindService(new WindowsFormsBindService());
            base.InitializeBindService(bindService);
        }

        /// <summary>
        /// The show view modal.
        /// </summary>
        /// <param name="view">
        /// The view.
        /// </param>
        protected override void ShowViewModal(object view)
        {
            var form = view as Form;
            if (form != null)
            {
                form.ShowDialog();
                return;
            }

            var dialog = view as CommonDialog;
            if (dialog != null)
            {
                dialog.ShowDialog();
                return;
            }
        }

        #endregion
    }
}